package cc.facesign.acl.controller;


import cc.facesign.acl.bean.AclMenu;
import cc.facesign.acl.bean.MenuTree;
import cc.facesign.acl.bean.vo.MenuEditVo;
import cc.facesign.acl.service.AclMenuService;
import cc.facesign.acl.service.TblUserService;
import cc.facesign.acl.utils.GaineUtils;
import cc.facesign.common.exception.FacesignException;
import cc.facesign.common.utils.bean.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

/**
 * <p>
 * 权限菜单 前端控制器
 * </p>
 *
 * @author acdongla
 * @since 2022-05-02
 */
@RestController
@RequestMapping("/acl/menu")
@Api(description = "权限菜单")
public class AclMenuController {

    @Autowired
    private AclMenuService menuService;

    @Autowired
    private TblUserService userService;

    @Autowired
    private GaineUtils gaineUtils;


    /**
     * 查菜单树（不包括admin）
     * @return
     */
    @ApiOperation("查所有菜单树（不包括admin）")
    @GetMapping("/getMenus")
    public R getMenus() {
        MenuTree tree = menuService.getMenuTreeByMenuId("1");

        return R.ok().data("tree", Collections.singletonList(tree));
    }

    /**
     * 根据菜单id查菜单树
     * @param menuId 菜单id
     * @return
     */
    @ApiOperation("查菜单树（根据menuId）")
    @GetMapping("/getTreeById/{menuId}")
    public R getTreeById(@PathVariable String menuId) {
        MenuTree tree = menuService.getMenuTreeByMenuId(menuId);
        return R.ok().data("data", Collections.singletonList(tree));
    }


    @ApiOperation("编辑菜单")
    @PreAuthorize("hasAuthority('root:update')")
    @PutMapping("/updateMenu/{menuId}")
    public R updateMenu(@PathVariable String menuId, @RequestBody MenuEditVo vo) {
        if (menuId.equals("0")) throw new FacesignException(R.ERROR, "权限异常");

        AclMenu menu = new AclMenu();
        menu.setId(menuId);
        BeanUtils.copyProperties(vo, menu);
        menuService.updateById(menu);

        return R.ok();
    }

}

